package com.rogo.rogobox.models
{
	import com.rogo.mvc.sub.proxy.Proxy;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.vo.CommentVO;
	import com.rogo.rogobox.models.vo.MicroblogVO;
	import com.rogo.rogobox.models.vo.RepostVO;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	
	/**
	 * 1.2 微博访问接口
	 * 
	 * @see http://open.weibo.com/wiki/API文档
	 * 
	 * @author rogo
	 */	
	public class MicroblogProxy extends Proxy
	{
		public function MicroblogProxy()
		{
			super(NAME);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'microblogProxy';
		
		//--------------------------------------------------------------------------
		//
		//  Varaibles
		//
		//--------------------------------------------------------------------------
		
		private var blog:BlogDelegate;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			blog = BlogDelegate.getInstance();
			blog.addEventListener(MicroBlogEvent.REPOST_STATUS_RESULT, blog_repostStatusResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.REPOST_STATUS_ERROR, blog_repostStatusErrorHandler);
			blog.addEventListener(MicroBlogEvent.COMMENT_STATUS_RESULT, blog_commentStatusResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.COMMENT_STATUS_ERROR, blog_commentStatusErrorHandler);
			blog.addEventListener(MicroBlogEvent.UPDATE_STATUS_RESULT, blog_updateStatusResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.UPDATE_STATUS_ERROR, blog_updateStatusErrorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * repost a microblog
		 */		
		public function repostStatus(vo:RepostVO):void
		{
			blog.repostStatus(vo.id, vo.status, vo.isComment);
		}
		
		/**
		 *	comment a microblog
		 */		
		public function commentStatus(vo:CommentVO):void
		{
			blog.commentStatus(vo.id, vo.comment, vo.cid);
		}
		
		/**
		 *	Publish microblogStatus
		 */		
		public function publishStatus(vo:MicroblogVO):void
		{
			blog.updateStatus(vo.status, vo.filename, vo.imgData);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function blog_repostStatusResultHandler(e:MicroBlogEvent):void
		{
			sendNotification(AppFacade.REPOST_MICROBLOG_RESULT);
			sendNotification(AppFacade.SHOW_TIP, '转发成功');
		}
		
		private function blog_repostStatusErrorHandler(e:MicroBlogErrorEvent):void
		{
			sendNotification(AppFacade.REPOST_MICROBLOG_ERROR);
			sendNotification(AppFacade.SHOW_TIP, '转发失败');
		}
		
		private function blog_commentStatusResultHandler(e:MicroBlogEvent):void
		{
			sendNotification(AppFacade.COMMENT_MICROBLOG_RESULT);
			sendNotification(AppFacade.SHOW_TIP, '评论成功');
		}
		
		private function blog_commentStatusErrorHandler(e:MicroBlogErrorEvent):void
		{
			sendNotification(AppFacade.COMMENT_MICROBLOG_ERROR);
			sendNotification(AppFacade.SHOW_TIP, '评论失败');
		}
		
		private function blog_updateStatusResultHandler(e:MicroBlogEvent):void
		{
			sendNotification(AppFacade.PUBLISH_MICROBLOG_RESULT);
			sendNotification(AppFacade.SHOW_TIP, '发布成功');
		}
		
		private function blog_updateStatusErrorHandler(e:MicroBlogErrorEvent):void
		{
			sendNotification(AppFacade.PUBLISH_MICROBLOG_ERROR);
			sendNotification(AppFacade.SHOW_TIP, '发布失败');
		}
		
	}
}